# THIS SCRIPT COMPUTES SUMMARY STATISTICS FOR FIGURE 13, AND PROVIDES
# THE SYNTAX FOR CREATING THE FIGURE ITSELF. IT MUST BE RUN IN CONJUNCTION 
# WITH THE DATASET "ANESCumulative_cleaned.csv", AVAILABLE IN THE REPLICATION 
# MATERIALS ON DATAVERSE. 
library(foreign)
library(car)
library(readstata13)
library(ggplot2)
library(stargazer)
library(grid)
library(gridExtra)
library(survey)
library(dplyr)
library(lemon)
library(ggpubr)
library(reshape2)

# READ IN THE DATA. SELECT THE FILE "ANESCumulative_cleaned.csv"
our.data = read.csv(file.choose())
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# ABORTION

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)

# POSITION, REPUBLICANS
our.means.republicans = svyby(~abortion, ~year + as.factor(programmatic), 
                              svymean, design = subset(our.design, republican==1), na.rm = TRUE)
our.means.republicans = as.data.frame(our.means.republicans)
# POSITION, DEMOCRATS
our.means.democrats = svyby(~abortion, ~year + as.factor(programmatic), 
                            svymean, design = subset(our.design, democrat==1), na.rm = TRUE)
our.means.democrats = as.data.frame(our.means.democrats)
# BIND THE TWO DATASETS
means.data = rbind(our.means.republicans, our.means.democrats)
names(means.data)[2] = c("programmatic")
levels(means.data$programmatic) = c("Unmatched", "Partially Matched", "Matched")
means.data$programmatic = factor(means.data$programmatic, levels = c("Matched",
                                                                     "Partially Matched",
                                                                     "Unmatched"))
means.data$partisanship = c(rep("Republicans", 60), 
                            rep("Democrats", 60))
# YEAR
means.data$year = as.numeric(as.character(means.data$year))
# REMOVE EMPTY YEARS; CLEAN UP
means.data.abortion = subset(means.data, abortion!=0)
means.data.abortion$item = rep("Abortion (average on \nfour-point index)", 
                               length(means.data.abortion$year))
names(means.data.abortion)[3] = "average"
head(means.data.abortion)
means.data.abortion = means.data.abortion[-c(4)]
head(means.data.abortion)
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# GUN CONTROL

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)

# PROPORTIONS, REPUBLICANS
our.props = svytable(~ guncontrol + programmatic + year, design = subset(our.design, republican==1))
republicans.props = prop.table(our.props, 2:3)
republicans.props = as.data.frame(republicans.props)
# PROPORTIONS, DEMOCRATS
our.props = svytable(~ guncontrol + programmatic + year, design = subset(our.design, democrat==1))
democrats.props = prop.table(our.props, 2:3)
democrats.props = as.data.frame(democrats.props)
# BIND THE TWO DATASETS
means.data = rbind(republicans.props, democrats.props)
means.data = subset(means.data, guncontrol==1)
means.data$partisanship = c(rep("Republicans", 18), 
                            rep("Democrats", 18))
# YEAR
means.data$year = as.numeric(as.character(means.data$year))
# PROGRAMMATIC
means.data$programmatic = car::recode(means.data$programmatic,
                                      "0 = 'Unmatched'; 0.5 = 'Partially Matched';
                                      1 = 'Matched'; else = NA")
# CLEAN UP DATA...
attach(means.data)
means.data.guncontrol = data.frame(year, programmatic, Freq, partisanship)
means.data.guncontrol$item = rep("Gun control (proportion that \nsupport more restrictions)", length(means.data.guncontrol$year))
names(means.data.guncontrol)[3] = "average"
detach(means.data)
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# MORAL TRADITIONALISM

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)

# POSITION, REPUBLICANS
our.means.republicans = svyby(~moraltrad, ~year + as.factor(programmatic), 
                              svymean, design = subset(our.design, republican==1), na.rm = TRUE)
our.means.republicans = as.data.frame(our.means.republicans)
# POSITION, DEMOCRATS
our.means.democrats = svyby(~moraltrad, ~year + as.factor(programmatic), 
                            svymean, design = subset(our.design, democrat==1), na.rm = TRUE)
our.means.democrats = as.data.frame(our.means.democrats)
# BIND THE TWO DATASETS
means.data = rbind(our.means.republicans, our.means.democrats)
names(means.data)[2] = c("programmatic")
levels(means.data$programmatic) = c("Unmatched", "Partially Matched", "Matched")
means.data$programmatic = factor(means.data$programmatic, levels = c("Matched",
                                                                     "Partially Matched",
                                                                     "Unmatched"))
means.data$partisanship = c(rep("Republicans", 60), 
                            rep("Democrats", 60))
# YEAR
means.data$year = as.numeric(as.character(means.data$year))
# REMOVE EMPTY YEARS; CLEAN UP
means.data.moraltrad = subset(means.data, moraltrad!=0)
means.data.moraltrad$item = rep("Moral traditionalism \n(average on four-item index)", 
                           length(means.data.moraltrad$year))
names(means.data.moraltrad)[3] = "average"
means.data.moraltrad = means.data.moraltrad[-c(4)]
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# MERGE 
new.data = rbind(means.data.abortion, 
                 means.data.guncontrol, 
                 means.data.moraltrad)
new.data

# PLOT
our.plot = ggplot(new.data,
                  aes(year, average, group = partisanship, 
                      linetype =  partisanship)) +
  geom_line(aes(linetype = partisanship), linewidth = 1) +
  labs(linetype = "Partisanship") + 
  xlab("Year") +
  # ylab("Average \n") +
  # ggtitle("Attitudes on Cultural Issues, 1980-2020, \nBy Partisanship and Matching Status", 
  #        subtitle = "Data: ANES cumulative file, partisan identifiers only.") + 
  theme_minimal() +  
  # scale_color_grey() +
  ylab("") + 
  facet_grid(programmatic ~ item, scales = "free_x") +
  # theme(legend.title = element_text(face = "bold")) +
  theme(legend.position = "none") +
  # theme(legend.title = element_text(size = 12)) +
  # theme(legend.text = element_text(size = 12)) +
  theme(panel.spacing = unit(1, "lines")) +
  theme(axis.title.x = element_text(size = 9)) + 
  theme(axis.title.y = element_text(size = 9)) +
  # theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 14)) + 
  # theme(plot.subtitle = element_text(hjust = 0.5, size = 12)) +
  theme(strip.text = element_text(face = "bold", size = 9)) +
  theme(panel.spacing = unit(1, "lines"))
our.plot

# SAVE
# ggsave(our.plot, file = "Schmidtetal-Figure13.pdf", width = 6.6, height = 9)
